<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 表单列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="page.name" placeholder="表单名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus-outline" @click="Add()">添加</el-button>
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="fromName" label="表单名称"></el-table-column>
                <el-table-column prop="fromJson" label="表单Json">
                    <template slot-scope="aaa">
                        {{aaa.row.fromJson.length>30? aaa.row.fromJson.substr(0,30)+'...':aaa.row.fromJson}}
                    </template>
                </el-table-column>
                <el-table-column prop="createName" label="创建人"></el-table-column>
                <el-table-column prop="fromDesc" label="表单详情"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="page.pageindex"
                    :page-size="page.pagesize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
//导入getUserList方法
import { getfrom,delefrom } from '@/api/from.js/';
import Tabs from '../../components/page/Tabs.vue';

export default {
    components: { Tabs },
    // name: 'basetable',
    data() {
        return {
            //地图基地址
            Img: 'http://localhost:5069/Image/',
            //用户列表查询
            page: {
                name: '',
                pageindex: 1,
                pagesize: 2
            },
            tableData: [], //用户信息集合
            tableRole: [], //角色表
            RoleName: [], //角色名称集合
            UseRole: [],
            //复选框
            multipleSelection: [],
            handleSelectionChangerole: [],
            delList: [],
            pageTotal: 0,
            form: {
                UserName: '',
                Phone: '',
                Emali: ''
            },
            idx: -1,
            id: -1,
            RoleUser: {
                RoleId: undefined,
                UserId: undefined
            },
            UserId: undefined,
            RoleId: undefined
        };
    },
    created() {
        this.getData();
    },
    methods: {
        handleEdit(index,row)
        {
            this.$router.push('FromUpdate?id='+row.id)
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            getfrom().then((res) => {
                this.tableData = res.data;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    delefrom(row.id).then((res) => {
                        if (res.code==200) {
                            this.$message.success('删除成功');
                            this.getData();
                        } else {
                            this.$message.success('删除失败');
                        }
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
            val.forEach((x) => {
                this.UserId = x.id;
                this.RoleId = x.roleId;
            });
            console.log(this.multipleSelection);
        },
        SelectionChangerole(val) {
            this.UseRole = val;
            console.log(this.UseRole);
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.page, 'pageindex', val);
            this.getData();
        },
        Add() {
            this.$router.push('/FromAdd');
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-UserImg {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
